package lumis.service.portalmanagement.channeltemplate;

import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.doui.source.TabularData;
import lumis.doui.source.postloadprocessor.IPostLoadProcessor;
import lumis.portal.PortalException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.PageConfig;
import lumis.portal.propertybag.Property;
import lumis.portal.propertybag.PropertyBag;
import lumis.util.ITransaction;

import java.util.List;

import org.w3c.dom.Node;

/**
 * Post load processor that loads the extra properties of the channel.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class ChannelTemplatePostLoadProcessor implements IPostLoadProcessor
{

	public void processSource(SessionConfig sessionConfig, Source source, Node parametersNode, ITransaction transaction) throws PortalException
	{
		TabularData tabularData = (TabularData) source.getData();
		for (ISourceData row : tabularData.getRows())
		{
			String id = row.get("channelId", String.class);
			ChannelConfig channel = ManagerFactory.getChannelManager().get(sessionConfig, id, transaction);
			PropertyBag propertyBag = ManagerFactory.getPropertyBagManager().get(channel.getPropertyBagId());

			// get the local layout file value
			try
			{
				Property localProperty = propertyBag.getLocalProperty(PageConfig.LAYOUT_FILE_PATH_PROPERTY_NAME);
				List<String> defaultValues = localProperty.getDefaultValues();
				if(defaultValues != null && !defaultValues.isEmpty())
				{
					String value = defaultValues.get(0);
					if(value != null)
						row.put("currentLayoutFile", value);
				}
			}
			catch (PortalObjectNotFoundException e)
			{
				// ignore if property is not present
			}
		}
	}
}
